<template>
  <div class="app-container">
    <!--查询参数列-->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="受检企业" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入受检企业"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="受检商品" prop="goodsName">
        <el-input
          v-model="queryParams.goodsName"
          placeholder="请输入受检商品"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="采样时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="检测状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择检测状态"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in statusSelect"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="采样类型" prop="sampleType" v-if="advanced">
        <el-select
          v-model="queryParams.sampleType"
          placeholder="请选择采样类型"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in sampleTypeSelect"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="信用代码" prop="uniformCode" v-if="advanced">
        <el-input
          v-model="queryParams.uniformCode"
          placeholder="请输入信用代码"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <a @click="toggleAdvanced" style="margin-left: 8px">
          {{ advanced ? '收起' : '展开' }}
          <i v-if="!advanced" class="el-icon-arrow-down"></i>
          <i v-if="advanced" class="el-icon-arrow-up"></i>
        </a>
      </el-form-item>
    </el-form>
    <!--表格顶部操作栏-->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:role:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single === true || enter === false"
          @click="handleUpdate"
          v-hasPermi="['system:role:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple === true || enter === false"
          @click="handleDelete"
          v-hasPermi="['system:role:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:role:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <!-- 表格数据-->
    <el-table v-loading="loading" :data="sampleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="采样编号" align="center" prop="sampleNo" :show-overflow-tooltip="true" width="200"/>
      <el-table-column label="受检企业" align="center" prop="companyName" :show-overflow-tooltip="true" width="120" />
      <el-table-column label="受检企业信用代码" align="center" prop="uniformCode" :show-overflow-tooltip="true" width="150" />
      <el-table-column label="受检商品" align="center" prop="goodsName" width="120" />
      <el-table-column label="检测项目" align="center" prop="itemName" :show-overflow-tooltip="true" width="240" />
      <el-table-column label="检测状态" align="center" prop="status" width="120">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.status === 1 ">已检测</el-tag>
          <el-tag type="danger" v-if="scope.row.status === 0 ">未检测</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="采样人" align="center" prop="sampler" width="120" />
      <el-table-column label="采样类型" align="center" prop="sampleType" width="150">
        <template slot-scope="scope">
          <span v-if="scope.row.sampleType === 'self' ">企业自检</span>
          <span v-if="scope.row.sampleType === 'task' ">任务检测</span>
          <span v-if="scope.row.sampleType === 'deliver' ">现场送检</span>
        </template>
      </el-table-column>
      <el-table-column label="采样时间" align="center" prop="sampleTime" width="150">
        <template slot-scope="scope">
          <span>{{ parseDateTime(scope.row.sampleTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="采样机构" align="center" prop="sampleOrgName" width="150" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="150">
        <template slot-scope="scope">
          <span>{{ parseDateTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" align="center" width="200px" class-name="small-padding fixed-width">
        <template slot-scope="scope" v-if="scope.row.roleId !== 1">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="handleDetail(scope.row)"
            v-hasPermi="['system:role:edit']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            :disabled="scope.row.status === 1"
            v-hasPermi="['system:role:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            :disabled="scope.row.status === 1"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:role:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 底部分页-->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 新增修改表单-->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="采样类型" prop="sampleType">
              <el-select v-model="form.sampleType" placeholder="请选择采样类型" clearable filterable @change="handleSampleTypeSelect"  style="width: 240px">
                <el-option
                  v-for="dict in sampleTypeSelect"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="采样编号" prop="sampleNo">
              <el-input v-model="form.sampleNo" placeholder="请输入采样编号"  style="width: 240px"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="所属任务" prop="task.taskId" v-if="this.form.sampleType === 'task'">
              <el-select v-model="form.task.taskId" placeholder="请选择所属任务" clearable filterable @change="handleTaskSelect" style="width: 240px">
                <el-option
                  v-for="dict in taskList"
                  :key="dict.taskId"
                  :label="dict.taskName"
                  :value="dict.taskId"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="任务编号" prop="task.taskNo" v-if="this.form.sampleType === 'task'">
              <el-input v-model="form.task.taskNo" placeholder="请输入任务编号" style="width: 240px"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="采样企业" prop="companyId">
              <el-select v-model="form.companyId" placeholder="请选择采样企业" clearable filterable @change="handleCompanySelect" style="width: 240px">
                <el-option
                  v-for="dict in companyList"
                  :key="dict.companyId"
                  :label="dict.companyName"
                  :value="dict.companyId"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="信用代码" prop="uniformCode">
              <el-input v-model="form.uniformCode" placeholder="请先选择受检企业"  style="width: 240px"/>
            </el-form-item>
          </el-col>
        </el-row>


        <el-row>
          <el-col :span="12">
            <el-form-item label="采样商品" prop="goodsId" >
              <el-select
                v-model="form.goodsId"
                filterable
                :remote="goodsSearch"
                reserve-keyword
                placeholder="请输入商品"
                :remote-method="searchGoods"
                @change="handleGoodsSelect"
                style="width: 240px"
              >
                <el-option
                  v-for="item in this.goodsList"
                  :key="item.goodsId"
                  :label="item.goodsName"
                  :value="item.goodsId">
                  <span style="float: left">{{ item.goodsName }}</span>
                  <span style="float: right; color: #8492a6; font-size: 13px">{{ item.ancestorsName }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="检测项目" prop="itemIds">
              <el-select
                v-model="form.itemIds"
                multiple
                filterable
                placeholder="请选择检测项目"
                style="width: 240px"
                @change="handleItemSelect"
              >
                <el-option
                  v-for="item in this.itemList"
                  :key="item.itemId"
                  :label="item.itemName"
                  :value="item.itemId">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="送检类型" prop="deliverType" v-if="this.form.sampleType === 'deliver'">
              <el-select v-model="form.deliver.deliverType" placeholder="请选择送检类型" clearable style="width: 240px">
                <el-option
                  v-for="dict in deliverTypeSelect"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="送检产地" prop="origin" v-if="this.form.sampleType === 'deliver'">
              <el-input v-model="form.deliver.origin" placeholder="请填写送检商品产地"  style="width: 240px"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="送检人" prop="deliverPeople" v-if="this.form.sampleType === 'deliver'">
              <el-input v-model="form.deliver.deliverPeople" placeholder="请填写送检人"  style="width: 240px"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="送检手机" prop="deliverPhone" v-if="this.form.sampleType === 'deliver'">
              <el-input v-model="form.deliver.deliverPhone" placeholder="请填写送检手机号"  style="width: 240px"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="采样时间" prop="sampleTime">
              <el-date-picker
                v-model="form.sampleTime"
                type="datetime"
                style="width: 240px"
                placeholder="选择采样时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="采样地址" prop="address">
              <el-input v-model="form.address" placeholder="请填写采样地址"  style="width: 240px"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="采样机构" prop="sampleOrgId">
              <el-select
                v-model="form.sampleOrgId"
                filterable
                style="width: 240px"
                @change="handleSampleOrgSelect"
              >
                <el-option
                  v-for="item in this.sampleOrgList"
                  :key="item.id"
                  :label="item.sampleOrgName"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="机构编码" prop="sampleOrgCode">
              <el-input v-model="form.sampleOrgCode" placeholder="请先选择机构编码"  style="width: 240px"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="采样人" prop="sampler" >
              <el-select v-model="form.samplerId" placeholder="请选择采样人" clearable filterable @change="handlerSamplerSelect"  style="width: 240px">
                <el-option
                  v-for="dict in samplerList"
                  :key="dict.id"
                  :label="dict.sampler"
                  :value="dict.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="签名" prop="samplerSign">
              <el-image
                style="width: 150px; height: 50px"
                :src="form.samplerSign"
                >
              </el-image>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入采样备注"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 详情页面-->
    <el-dialog title="采样详情" :visible.sync="detailOpen" width="700px" append-to-body>
      <el-descriptions class="margin-top" :column="2" border title="采样单">
        <el-descriptions-item label="采样编号">{{ form.sampleNo }}</el-descriptions-item>
        <el-descriptions-item label="采样类型">
          <span v-if="form.sampleType === 'self'">企业自检</span>
          <span v-if="form.sampleType === 'task'">任务检测</span>
          <span v-if="form.sampleType === 'deliver'">现场送检</span>
        </el-descriptions-item>
        <el-descriptions-item label="企业名称">{{ form.companyName }}</el-descriptions-item>
        <el-descriptions-item label="信用代码">{{ form.uniformCode }}</el-descriptions-item>
        <el-descriptions-item label="送检类型" v-if="this.form.sampleType === 'deliver'">
          <span v-if="form.deliver.deliverType === 'company'">企业送检</span>
          <span v-if="form.deliver.deliverType === 'customer'">消费者送检</span>
        </el-descriptions-item>
        <el-descriptions-item label="送检产地" v-if="this.form.sampleType === 'deliver'">{{ form.deliver.origin }}
        </el-descriptions-item>
        <el-descriptions-item label="送检人" v-if="this.form.sampleType === 'deliver'">
          {{ form.deliver.deliverPeople }}
        </el-descriptions-item>
        <el-descriptions-item label="送检手机" v-if="this.form.sampleType === 'deliver'">
          {{ form.deliver.deliverPhone }}
        </el-descriptions-item>

        <el-descriptions-item label="采样时间">{{ parseDateTime(form.sampleTime) }}</el-descriptions-item>
        <el-descriptions-item label="采样地址">{{ form.address }}</el-descriptions-item>
        <el-descriptions-item label="采样机构">{{ form.sampleOrgName }}</el-descriptions-item>
        <el-descriptions-item label="机构编码">{{ form.sampleOrgCode }}</el-descriptions-item>
        <el-descriptions-item label="采样人">{{ form.sampler }}</el-descriptions-item>
        <el-descriptions-item label="采样人签名">
          <el-image
            style="width: 150px; height: 50px"
            :src="form.samplerSign"
          >
          </el-image>
        </el-descriptions-item>
        <el-descriptions-item label="采样备注" :span="2">{{ form.remark }}</el-descriptions-item>
      </el-descriptions>

      <el-descriptions v-if="form.status === 1" class="margin-top" style="padding-top: 20px" :column="2" border title="检测单">
        <el-descriptions-item v-if="form.status === 1" label="检测编码">{{ checkData.checkBatchNo }}
        </el-descriptions-item>
        <el-descriptions-item v-if="form.status === 1" label="检测人">{{ checkData.checker }}</el-descriptions-item>
        <el-descriptions-item v-if="form.status === 1" label="检测方法">
          <span v-if="checkData.checkMethod === 1">酶抑制法</span>
          <span v-if="checkData.checkMethod === 2">分光光度计</span>
          <span v-if="checkData.checkMethod === 3">化学比色法</span>
          <span v-if="checkData.checkMethod === 4">胶体金免疫法</span>
        </el-descriptions-item>
        <el-descriptions-item v-if="form.status === 1" label="检测类型">
          <span v-if="checkData.checkType === 1">定性</span>
          <span v-if="checkData.checkType === 2">定量</span>
        </el-descriptions-item>
        <el-descriptions-item v-if="form.status === 1" label="检测机构">{{ checkData.checkOrgName }}
        </el-descriptions-item>
        <el-descriptions-item v-if="form.status === 1" label="检测机构编码">{{ checkData.checkOrgCode }}
        </el-descriptions-item>
        <el-descriptions-item :span="2" v-if="form.status === 1" label="检测结果">
          <el-tag type="success" v-if="checkData.result === 1">阴性（合格）</el-tag>
          <el-tag type="danger" v-if="checkData.result === 2">阳性（不合格）</el-tag>
        </el-descriptions-item>
        <template v-for="(item, index) in checkData.itemList">
          <el-descriptions-item :key="index + 'name'">
            <template slot="label">
              检测项目{{ index + 1 }}
            </template>
            <span>{{ item.itemName }}</span>
          </el-descriptions-item>


          <el-descriptions-item :key="index + 'result'">
            <template slot="label">
              检测结果
            </template>
            <el-tag type="success" v-if="item.result === 1">阴性（合格）</el-tag>
            <el-tag type="danger" v-if="item.result === 2">阳性（不合格）</el-tag>
          </el-descriptions-item>
        </template>
      </el-descriptions>
    </el-dialog>
  </div>


</template>

<script>


import {
  deleteSample,
  getSample,
  listSample,
  listSampler,
  randomSampleNo,
  saveSample,
  updateSample
} from "@/api/detection/sample/sample";
import {listCompany} from "@/api/subject/company/company";
import {listGoods} from "@/api/goods/goods";
import {listItem} from "@/api/item/item";
import {listSampleOrg} from "@/api/subject/sampleOrg/sampleOrg";
import {parseDateTime} from "@/utils/moss";
import {getCheck} from "@/api/detection/check/check";
import {getTask, listTask} from "@/api/detection/task/task";

export default {
  name: "index",
  data() {
    return {
      showSearch: true,
      advanced: true,
      // 日期范围
      dateRange: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 是否允许修改，已录入的不允许修改
      enter: true,
      // 页面加载，默认关闭
      loading: false,
      // 新增修改表单弹窗，默认关闭
      open: false,
      // 采样详情弹窗，默认关闭
      detailOpen: false,
      total: 0,
      sampleList:[],
      companyList:[],
      samplerList:[],
      goodsList:[],
      itemList:[],
      sampleOrgList: [],
      taskList: [],
      goodsSearch: true,
      title: '',
      form: {},
      checkData: {},
      queryParams: {
        companyName: undefined,
        uniformCode: undefined,
        status: undefined,
        goodsName: undefined,
        sampleType: undefined,
        pageNum: 1,
        pageSize: 10
      },
      statusSelect: [
        {
          label: "已检测",
          value: 1
        },
        {
          label: "未检测",
          value: 0
        }
      ],
      sampleTypeSelect: [
        {
          label: "企业自检",
          value: "self"
        },
        {
          label: "现场送检",
          value: "deliver"
        },
        {
          label: "任务检测",
          value: "task"
        }
      ],
      deliverTypeSelect: [
        {
          label: "企业送检",
          value: "company"
        },
        {
          label: "消费者送检",
          value: "customer"
        }
      ],
      rules: {
        companyId: [
          { required: true, message: "受检企业不能为空", trigger: "blur" }
        ],
        uniformCode: [
          { required: true, message: "受检企业信用代码不能为空", trigger: "blur" }
        ],
        sampleType: [
          { required: true, message: "检测类型不能为空", trigger: "blur" }
        ],
        sampleNo: [
          { required: true, message: "采样编号不能为空", trigger: "blur" }
        ],
        'task.taskId': [
          { required: true, message: "所属任务不能为空", trigger: "blur" }
        ],
        'task.taskNo': [
          { required: true, message: "所属任务编码不能为空", trigger: "blur" }
        ],
        goodsId: [
          { required: true, message: "采样商品不能为空", trigger: "blur" }
        ],
        itemIds: [
          { required: true, message: "检测项目不能为空", trigger: "blur" }
        ],
        sampleTime: [
          { required: true, message: "采样商品不能为空", trigger: "blur" }
        ],
        sampleOrgId: [
          { required: true, message: "采样商品不能为空", trigger: "blur" }
        ],
        sampleOrgCode: [
          { required: true, message: "检测项目不能为空", trigger: "blur" }
        ],
        sampler: [
          { required: true, message: "采样商品不能为空", trigger: "blur" }
        ],
        samplerSign: [
          { required: true, message: "检测项目不能为空", trigger: "blur" }
        ],
        deliverPeople: [
          { required: true, message: "送检人不能为空", trigger: "blur" }
        ],
        deliverPhone: [
          { required: true, message: "送检手机号不能为空", trigger: "blur" },
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ],
        deliverType: [
          { required: true, message: "送检类型不能为空", trigger: "blur" },
        ]
      },
    }
  },
  created() {
    this.initSelectTime();
    this.getList()
  },
  methods: {
    getList(){
      this.loading = true;
      listSample(this.buildQueryTime(this.queryParams, this.dateRange)).then(response => {
          this.sampleList = response.data;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    handleCompanySelect(value){
      let selectCompany = this.companyList.find(option => option.companyId === value);
      this.form.companyName = selectCompany.companyName;
      this.form.uniformCode = selectCompany.uniformCode;
    },
    handleTaskSelect(value){
      let selectTask = this.taskList.find(option => option.taskId === value);
      this.$set(this.form.task,'taskId',selectTask.taskId)
      this.$set(this.form.task,'taskNo',selectTask.taskNo)
      this.$set(this.form.task,'taskName',selectTask.taskName)
      // this.form["task"].taskId = selectTask.taskId;
      // this.form["task"].taskNo = selectTask.taskNo;
      // this.form["task"].taskName = selectTask.taskName;
      getTask(selectTask.taskId).then(res => {
        this.companyList = res.data.companyList;
        this.goodsList = res.data.goodsList;
      })
    },
    handleSampleTypeSelect(value){
      randomSampleNo(value).then(res=>{
        this.form.sampleNo = res.data;
      })
      this.goodsList = []
      this.goodsSearch = value !== 'task';
    },
    handlerSamplerSelect(value){
      const sampler = this.samplerList.find(opt => opt.id === value);
      this.form.sampler = sampler.sampler;
      this.form.samplerSign = sampler.samplerSign;
    },
    handleGoodsSelect(value){
      const goods = this.goodsList.find(opt => opt.goodsId === value);
      this.form.goodsId = goods.goodsId;
      this.form.goodsName = goods.goodsName;
      if (this.form.sampleType === 'task'){
        this.itemList = goods.itemList;
        this.form.itemIds = goods.itemList.map(o => o.itemId)
        this.form.itemList = goods.itemList
      }
    },
    handleItemSelect(values){
      this.form.itemList = this.itemList.filter(obj => values.includes(obj.itemId));
    },
    handleSampleOrgSelect(value){
      const sampleOrg = this.sampleOrgList.find(opt => opt.id === value);
      this.form.sampleOrgCode = sampleOrg.sampleOrgCode
      this.form.sampleOrgName = sampleOrg.sampleOrgName
    },
    initSelectTime(){
      const end = new Date();
      end.setHours(23,59,59);
      this.dateRange[1] = end;

      const start = new Date();
      start.setDate(start.getDate() - 6);
      start.setHours(0,0,0);
      this.dateRange[0] = start
    },
    searchGoods(query){
      const goods = {"goodsName":query};
      listGoods(goods).then(res => {
        this.goodsList = res.data;}
      )
    },
    handleQuery(){
      this.queryParams.pageNum = 1;
      this.queryParams.pageSize = 10;
      this.getList();
    },
    checkDataReset(){
      this.checkData = {};
    },
    handleDetail(row){
      this.reset();
      this.checkDataReset();
      const sampleId = row.sampleId || this.ids;
      const type = row.sampleType;
      getSample(type,sampleId).then(res => {
        this.form = res.data;
        if (this.form.status === 1){
          getCheck(this.form.sampleId).then(res => {
            this.checkData = res.data;
          })
        }
        this.detailOpen = true;
      })
    },
    resetQuery(){
      this.queryParams = {
        pageNum: 1,
        pageSize: 10
      }
    },
    handleAdd(){
      this.reset();
      listCompany().then(res =>{
        this.companyList = res.data;
      });
      listSampler().then(res => {
        this.samplerList = res.data;
      })
      listItem().then(res => {
        this.itemList = res.data;
      })
      listSampleOrg().then(res => {
        this.sampleOrgList = res.data;
      })
      listTask().then(res => {
        this.taskList = res.data;
      })
      this.open = true;
      this.title = "采样录入";
    },
    handleUpdate(row){
      this.reset();
      listCompany().then(res =>{
        this.companyList = res.data;
      });
      listSampler().then(res => {
        this.samplerList = res.data;
      })
      listItem().then(res => {
        this.itemList = res.data;
      })
      listSampleOrg().then(res => {
        this.sampleOrgList = res.data;
      })
      const sampleId = row.sampleId || this.ids;
      const type = row.sampleType;
      getSample(type,sampleId).then(res => {
        this.form = res.data;
        this.form.itemIds = this.form.itemId.split(",").map(Number);
        const goods = {"goodsName":this.form.goodsName};
        listGoods(goods).then(res => {
          this.goodsList = res.data
        })
        this.open = true;
        this.title = '修改采样';
      });
    },
    handleDelete(row){
      const sampleIds = row.sampleId || this.ids;
      this.$modal.confirm('是否确认删除？').then(function() {
        return deleteSample(sampleIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    handleExport(){

    },
    submitForm(){
      this.$nextTick(() => {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.sampleId !== undefined) {
              updateSample(this.form).then(res => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              })
            } else {
              saveSample(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      });

    },
    cancel() {
      this.open = false;
      this.reset();
    },
    toggleAdvanced () {
      this.advanced = !this.advanced
    },
    reset(){
      this.form = {
        sampleId: undefined,
        companyName: undefined,
        uniformCode: undefined,
        sampleType: "",
        sampleNo: "",
        sampler:"",
        samplerSign: "",
        sampleTime: parseDateTime(new Date()),
        goodsId: undefined,
        goodsName: undefined,
        itemIds: [],
        itemList: [],
        sampleOrgId: undefined,
        sampleOrgCode: "",
        sampleOrgName: "",
        remark:"",
        deliver: {
          deliverType:"",
          deliverPeople:"",
          deliverPhone:"",
          origin: ""
        },
        task: {
          taskId: "",
          taskName: "",
          taskNo: ""
        }
      };
      this.resetForm("form");
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.sampleId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
      this.enter = !selection.some(item => item.status === 1);
    }
  }
}
</script>

<style scoped>

</style>
